package com.hunttown.mes.manage.controller._basic;

import com.hunttown.mes.common.OperateCode;
import com.hunttown.mes.manage.service.TransportUploadFilesManageService;
import com.hunttown.mes.rpc.domain.TransportUploadFilesDTO;
import com.hunttown.mes.rpc.domain.enums.upload.ServiceTypeEnum;
import com.hunttown.mes.rpc.domain.query.TransportUploadFilesDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.JSONUtils;
import com.hunttown.mes.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

import static com.hunttown.mes.common.utils.PageUtils.PagePackage;

/**
 * @title: 文件上传
 * @author: wangjunfu
 * @date: 2020-06-14 12:47:46
 * @description:
 */
@Controller
@RequestMapping(value = "/basic/upload-file")
public class UploadFilesController {

    private final static Logger logger = LoggerFactory.getLogger(UploadFilesController.class);

    private final TransportUploadFilesManageService uploadFilesService;

    @Autowired
    public UploadFilesController(TransportUploadFilesManageService uploadFilesService) {
        this.uploadFilesService = uploadFilesService;
    }

    /**
     * 首页
     *
     * @param model    model模型
     * @param dtoQuery 查询条件
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model, TransportUploadFilesDTOQuery dtoQuery) {

        dtoQuery.setOrderBy("order by id desc");
        Page<TransportUploadFilesDTO> dataPage = uploadFilesService.getForPage(dtoQuery);

        for (TransportUploadFilesDTO item : dataPage.getItems()) {
            item.setFileSize(item.getFileSize() / 1000);
            item.setServiceTypeName(ServiceTypeEnum.getEnumByKey(item.getServiceType()));

            String pic = GeneralUtils.getContentTypeUrl(item.getContentType());
            if (StringUtils.isBlank(pic)) {
                pic = item.getWebUrl();
            }
            item.setIconUrl(pic);
        }

        model.addAttribute("page", dataPage);
        model.addAttribute("PagePackage", PagePackage(dataPage.getPn(), dataPage.getStart(), dataPage.getPageSize(), dataPage.getTotalPages(), dataPage.getTotalItems()));
        model.addAttribute("dtoQuery", dtoQuery);

        return "basic/upload-file/index";
    }

    /**
     * 通过ID删除 ajax异步
     *
     * @param id 记录id
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public String deleteNotify(Integer id) {

        // 结果返回结果至少包含一个操作状态和一条状态信息
        String result = "";
        Map<String, String> map = new HashMap<String, String>();

        if (id == null || id <= 0) {
            map = OperateCode.CompositionResult(map, -10, "请选择要删除的记录！");
            result = JSONUtils.toJSON(map);
            return result;
        }

        try {
            Boolean isNext = uploadFilesService.delete(id);
            map = OperateCode.CompositionResult(map, isNext ? 1 : 0);
            result = JSONUtils.toJSON(map);
            return result;

        } catch (Exception e) {
            //清空map
            map.clear();
            map = OperateCode.CompositionResult(map, -1);
            result = JSONUtils.toJSON(map);
            return result;
        }
    }

    /**
     * 数据查看
     *
     * @param model     model模型
     * @param id        主键id
     * @param tableName 数据库英文表名
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public String show(Model model, Integer id, String tableName) {
        id = (id == null ? 0 : id);

        TransportUploadFilesDTO obj = new TransportUploadFilesDTO();
        obj.setId(id);

        if (id > 0) {
            obj = uploadFilesService.getById(id);
        }
        uploadFilesService.assembleObjInfo(model, tableName, obj);

        return "show/show-template";
    }
}